home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / editors / mutt / me2s_pl7.zoo / mu_edit2 / util / dstring.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-05  |  3.5 KB  |  140 lines

  1. static char rcsid[] = "$Id: dstring.c,v 1.1 1992/09/06 19:31:32 mike Exp $";
  2.  
  3. /* $Log: dstring.c,v $
  4.  * Revision 1.1  1992/09/06  19:31:32  mike
  5.  * Initial revision
  6.  *
  7.  */
  8.  
  9. /* dstring.c: dynamic strings.
  10.  * C Durland    Public Domain
  11.  */
  12.  
  13. #include "const.h"
  14. #include "dstring.h"
  15.  
  16. extern char *malloc(), *realloc(), *strcpy();
  17.  
  18. static char empty_string[1] = { '\0' };
  19.  
  20.     /* Note:  You must initialize a dString.  If it is declared as a
  21.      *   global var, it is initialized enough if you set it before you
  22.      *   get it.
  23.      */
  24. void init_dString(ds) register dString *ds;
  25. {
  26.   ds->size = 0; ds->string = empty_string;
  27. }
  28.  
  29.     /* Returns: TRUE is ok, FALSE if could not malloc (string set to
  30.      *   empty_string).
  31.      */
  32. set_dString(ds,text) register dString *ds; char *text;
  33. {
  34.   if (!pad_dString(ds,strlen(text))) return FALSE;
  35.  
  36.   strcpy(ds->string,text);
  37.  
  38.   return TRUE;
  39. }
  40.  
  41. void free_dString(ds) register dString *ds;
  42. {
  43.   if (ds->size) free(ds->string);
  44.   init_dString(ds);
  45. }
  46.  
  47.     /* Make sure dString has enough space in it to hold a string of length
  48.      *   len.  Also leave room for the \0.
  49.      * Notes:
  50.      *   dString contents are sometimes trashed by this routine.
  51.      *   The location of the text might be moved so don't say 
  52.      *     ptr = ds->string, pad_dString(ds) and expect ptr to be valid!
  53.      *   Some routines will want the orginal contents to be intact after the
  54.      *     pad so that can do things like strcat().
  55.      *   !!!Under Ansi C, I'll need to free the dstring if the realloc()
  56.      *     fails.  Of course this doesn't work everywhere so I'll blow it
  57.      *     off.
  58.      * Input:
  59.      *   ds  : Pointer to dString that needs some room
  60.      *   len : Strlen of string that ds needs to be able to hold.
  61.      * Output:
  62.      *   ds : Expanded dString.  Freed and initialized if no memory (ie old
  63.      *     contents are gone).
  64.      * Returns:
  65.      *   TRUE:   Everything went as expected.
  66.      *   FALSE:  Out of memory.
  67.      */
  68. pad_dString(ds,len) register dString *ds; int len;
  69. {
  70.   char *ptr;
  71.   int size = ds->size;
  72.  
  73.   len++;
  74.   if (len <= size) return TRUE;
  75.  
  76.   if (size)        /* dString has already been malloc()ed */
  77.     ptr = realloc(ds->string, len);
  78.   else  ptr = malloc(len);
  79.  
  80.   if (!ptr) { init_dString(ds); return FALSE; }
  81.  
  82.   ds->size = len;
  83.   ds->string = ptr;
  84.  
  85.   return TRUE;
  86. }
  87.  
  88. #if 0
  89. char *malloc(), *realloc(), *strcat();
  90. cat_dString(ds,string) register dString *ds; char *string;
  91. {
  92.   register char *ptr = ds->string;
  93.   register int n = strlen(string) +1, size = ds->size;
  94.  
  95.   if (size<n)    /* expand string */
  96.   {
  97.     ptr = size ? realloc(ptr,n) : malloc(n);
  98.     if (ptr == NULL) { init_dString(ds); return 0; }
  99.     if (!size) *ptr = '\0';
  100.     ds->size = n;
  101.   }
  102.   ds->string = strcat(ptr,string);
  103.   return 1;
  104. }
  105. #endif
  106.  
  107.  
  108. #ifdef TEST
  109. /* ******************************************************************** */
  110. /* ***********  TESTING *********************************************** */
  111. /* ******************************************************************** */
  112.  
  113. #include <stdio.h>
  114.  
  115. main()
  116. {
  117.   char buf[200];
  118.   dString ds;
  119.  
  120.   init_dString(&ds);
  121.   while (1)
  122.   {
  123.     printf(">>>>s(set string), p(print string), q(quit): ");
  124.     gets(buf); puts("");
  125.     switch (*buf)
  126.     {
  127.       case 's':    /* set string */
  128.     printf("string: "); gets(buf); puts("");
  129.     set_dString(&ds,buf);
  130.     break;
  131.       case 'p':    /* print string */
  132.     printf(">%s<\n",ds.string);
  133.     break;
  134.       case 'q': goto done;
  135.     }
  136.   }
  137.   done: ;
  138. }
  139. #endif
  140.